#include "./componet.hh"
#include <format>
#include <sstream>


namespace ycspice::preprocess {

    auto componettype_to_char(ComponetType type) -> char {
        switch (type) {
            case ComponetType::Capacitance: return 'c';
            case ComponetType::Resistance:  return 'r';
            case ComponetType::Inductance:  return 'l';
        }
        std::exit(1);
    }

    auto Componet::componet_name(ComponetType type, std::string value) -> std::string {
        return std::format("{}{}", componettype_to_char(type), value);
    }

    Componet::Componet(ComponetType type, std::string value) :
        Circuit{Componet::componet_name(type, value)},
        _value{std::move(value)}
    {
        this->add_port("p1");
        this->add_port("p2");
    }

    auto Componet::expand_spice(std::string_view inst_name, std::span<std::string> nets) const -> std::string {
        // std::println("expand_spice for '{}'", this->_name);
        this->check_net_size(nets);
        
        auto ss = std::stringstream{};
        ss << inst_name;
        for (auto net : nets) {
            ss << ' ' << net;
        }
        ss << ' ' << this->_value;

        return ss.str();
    }

}